package com.sh4.wxservice;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sh4.entity.WxUser;
import com.sh4.util.EmojiFilter;
import com.sh4.util.HttpUtil;
import com.sh4.util.JackJsonUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 服务号 粉丝
 * Created by Administrator on 2017/7/7.
 */
public class User {


    /**
     * 设置用户备注名
     */
    public static Boolean updateRemark(String openId, String name,String accessToken)
    {
        String url = "https://api.weixin.qq.com/cgi-bin/user/info/updateremark?access_token="
                + accessToken;
        Map<String , Object> json = new HashMap<String , Object>();
        json.put("openid", openId);
        json.put("remark", name);
        JSONObject rs = null;
        try{
            rs =  JSONObject.fromObject(HttpUtil.doPost(url,json));
        }catch (Exception e){
        }
        return rs.getString("errcode") == "0" ? true : false;
    }

    /**
     * 获取用户基本信息   
     * 
     * @param openId
     * @param lang ( zh_CN 简体，zh_TW 繁体，en 英语)
     * @param accessToken 该公司的accessToken
     * 
     */
    public static WxUser userInfo(String openId, String lang,String accessToken) {
    	
        String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="
                + accessToken
                + "&openid="
                + openId
                + "&lang="
                + lang;
        JSONObject rs = null;
        WxUser info = new WxUser();
        try{
        	rs = JSONObject.fromObject(HttpUtil.doGet(url));
        	return getWxInfo(rs);
        }catch (Exception e){
        	return info;
        }
       
    }
    /**
     * 将接口获取的用户信息 转为 微信实体
     * @param rs
     * @return
     */
    public static WxUser getWxInfo(Object ob){
    	WxUser info = new WxUser();
    	JSONObject rs = JSONObject.fromObject(ob);
    	if(rs.containsKey("errcode")){
	   		 return info;
	   	}
	   	Long time = rs.getLong("subscribe_time");
	   	rs.remove("subscribe_time");
	   	String tagid_list = rs.getString("tagid_list").replaceAll("[\\[\\]]", ""),
	   			subscribe_scene = rs.getString("subscribe_scene"),
	   			qr_scene_str = rs.getString("qr_scene_str"),
	   					qr_scene = rs.getString("qr_scene");
	   	
	   	rs.remove("tagid_list");
	   	rs.remove("subscribe_scene");
	 	rs.remove("qr_scene_str");
	 	rs.remove("qr_scene");
	   	info =  JackJsonUtils.fromJson(rs.toString(), WxUser.class);
	   	info.setSubscribeTime(new Date(time * 1000));
	   	info.setTagidList(tagid_list);
	   	info.setSubscribeScene(subscribe_scene);
	   	info.setQrSceneStr(qr_scene_str);
	   	info.setQrScene(qr_scene);
	   	info.setNickname(EmojiFilter.filterEmoji(info.getNickname()));
		return info;
    }
    /**
     * 批量获取用户基本信息 lang( zh_CN 简体，zh_TW 繁体，en 英语)
     * @param info  [openid]
     * @param accessToken
     * @param lang   lang( zh_CN 简体，zh_TW 繁体，en 英语)
     * @return
     * @throws Exception
     */
//    public static JSONObject GetInfoBatchs(JSONArray openids, String accessToken,String lang)
//            throws Exception {
//        String url = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token="
//                + accessToken;
//        StringBuffer json = new StringBuffer();
//        json.append("{");
//        json.append("\"user_list\": [");
//        for(Object openid:openids){
//        	json.append("{");
//        	json.append("\"openid\": \""+openid+"\",");
//            json.append("\"lang\": \""+lang+"\"");
//            json.append("}, ");
//        }
//        json.deleteCharAt(json.length()-1);
//        json.append("]");
//        json.append("}");
//        JSONObject rs = null;
//        try{
//            rs =  JSONObject.fromObject(RequestUtil.doPostJson(json.toString(), url));
//        }catch (Exception e){
//        }
//        return rs;
//    }
    /**
     * 批量获取用户基本信息 lang( zh_CN 简体，zh_TW 繁体，en 英语)
     */
//    public static JSONObject GetInfoBatch(List<Map<String,Object>> info, String accessToken)
//            throws Exception {
//        String url = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token="
//                + accessToken;
//        StringBuffer json = new StringBuffer();
//        json.append("{");
//        json.append("\"user_list\": [");
//        json.append("{");
//        for(Map<String,Object> map : info){
//            json.append("\"openid\": \""+map.get("openid")+"\",");
//            json.append("\"lang\": \""+map.get("lang")+"\"");
//            json.append("}, ");
//        }
//        json.deleteCharAt(json.length()-1);
//        json.append("]");
//        json.append("}");
//        JSONObject rs = null;
//        try{
//            rs =  JSONObject.fromObject(RequestUtil.doPostJson(json.toString(), url));
//        }catch (Exception e){
//        }
//        return rs;
//    }

    /**
     * 获取用户列表
     * @return   {"total":2,"count":2,"data":{"openid":["","OPENID1","OPENID2"]},"next_openid":"NEXT_OPENID"}
     * @throws Exception
     */
//    public static JSONObject getUserList(String nextOpenId,String accessToken)
//            throws Exception {//第一个拉取的OPENID，不填默认从头开始拉取
//        String url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token="+
//                accessToken+"&next_openid="+nextOpenId;
//        JSONObject rs = null;
//        try{
//            rs =  JSONObject.fromObject(RequestUtil.doGet(url));
//        }catch (Exception e){
//        }
//        return rs;
//    }

}
